package fr.labri.buganalyzer.eclipse;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class EclipseIssuesAnalysis {

	public static final String PRODUCT_LIST_URL = "https://bugs.eclipse.org/bugs/describecomponents.cgi";
	public static final String PRODUCT_TOKEN = "?product=";
	public static final int TIMEOUT = 10000;

	private List<String> productList = new ArrayList<>();
	private Map<String, List<String>> productTree = new HashMap<>();

	public final void downloadProductList() {
		try {
			Document doc = Jsoup.connect(PRODUCT_LIST_URL).timeout(TIMEOUT).get();
			Element element = doc.getElementById("bugzilla-body");

			for (Element table : element.select("table")) {
				for (Element row : table.select("tr")) {
					Elements tds = row.select("th");
					String productName = tds.get(0).text();
					getProductList().add(prepareProductName(productName));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String prepareProductName(String productName) {
		return productName.replace(":", "").replaceAll("\u00A0", " ").trim();
	}

	public final void downloadProductsTree() {
		List<String> compList = null;
		for (String p : getProductList()) {
			try {
				String pName = p.replaceAll(" ", "%20");
				Document doc = Jsoup.connect(PRODUCT_LIST_URL + PRODUCT_TOKEN + pName).timeout(TIMEOUT).get();
				Element element = doc.getElementById("bugzilla-body");
				

				compList = new ArrayList<>();
				for (Element table : element.select("tbody")) {
					for (Element row : table.select("tr")) {
						String componentName = row.id();
						if (!componentName.isEmpty()) compList.add(componentName);
					}
				}	
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("Product : " + p + "\n" + compList);
			productTree.put(p, compList);
		}	
	}
	
	
	public final void writeToJsonFile() throws IOException {
				
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
		mapper.writeValue(new File("resources/testData/EclpiseProjectsInBugzilla.json"), productTree);
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		EclipseIssuesAnalysis analyzer = new EclipseIssuesAnalysis();
		analyzer.downloadProductList();
		analyzer.downloadProductsTree();
		analyzer.writeToJsonFile();

	}


	public final List<String> getProductList() {
		return productList;
	}


	public final void setProductList(List<String> productList) {
		this.productList = productList;
	}

	public final Map<String, List<String>> getProductTree() {
		return productTree;
	}

	public final void setProductTree(Map<String, List<String>> productTree) {
		this.productTree = productTree;
	}

}
